//
//  Connection.m
//  tucici
//
//  Created by SKL on 2017/11/6.
//  Copyright © 2017年 SKL. All rights reserved.
//
//#define HTTP_HEADER @"http://192.168.1.25/skylineSDKserver/api"
#import "XOWMHttps.h"
#import "XOWMPlatform.h"
#import "NSDictionary+category.h"
@interface XOWMHttps() <NSURLSessionDelegate>

@end
@implementation XOWMHttps

// 使用拼接字符串方式，请求post
+ (NSData *)yjd_convertToString:(NSDictionary *)dict
{
    return [[dict yjd_urlCodeString] dataUsingEncoding:NSUTF8StringEncoding];
}

+ (NSMutableURLRequest *)yjd_getURLRequest:(NSString *)api httpBody:(id)httpBody
{
    
    NSString *newAPI = ([api containsString:@"http"])?api:[NSString stringWithFormat:@"%@%@",helperManager().getPara().serviceHeader, api];
    //创建一个url
    NSURL *url = [NSURL URLWithString:newAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = ([httpBody isKindOfClass:[NSDictionary class]])?[self yjd_convertToString:httpBody]:httpBody;    //如果是用json请求 要设置application/json Content-Type
    request.timeoutInterval = 10;
    //    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    return request;
}

+ (NSURLSessionTask *)post:(NSString *)api parameters:(id)para delegate:(id)delegateObject
{
    //创建回话类型
    //    1. defaultSessionConfiguration 默认使用持久化的硬盘缓存，存储证书到用户钥匙链。存储cookie到shareCookie。
    //    2. ephemeralSessionConfiguration --------->不永久持存cookie，证书，缓存，存在内存中，程序退出就消失，秘密浏览
    //    3. backgroundSessionConfigurationWithIdentifier------>程序在后台运行下载
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    //创建一个会话对象
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:delegateObject delegateQueue:[[NSOperationQueue mainQueue]init]];
    //创建一个会话任务
    NSURLSessionTask *task = [session dataTaskWithRequest:[self yjd_getURLRequest:api httpBody:para]];
    //开启任务
    
    [task resume];
    return task;
}

+ (NSURLSessionTask *)get:(NSString *)api
{
    //创建一个url
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",helperManager().getPara().serviceHeader, api]];
    
    //创建一个request对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //创建一个会话对象
    NSURLSession *session = [NSURLSession sharedSession];
    //创建一个会话任务
    //    NSURLSessionTask *task = [session dataTaskWithRequest:request];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error)
            DeLog(@"get request error");
        else
            DeLog(@"get 请求  data %@  ", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]);
    }];
    //开启任务
    [task resume];
    return task;
}

+ (NSURLSessionTask *)post:(NSString *)api parameters:(id)para complete:(complete)complete error:(error)errors
{
    NSURLSession *session = [NSURLSession sharedSession];
    __block NSURLSessionTask *task = [session dataTaskWithRequest:[self yjd_getURLRequest:api httpBody:para] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                                      {
                                          if (error)
                                          {
                                              errors(error);
                                          }
                                          else
                                          {
                                              complete(data);
                                          }
                                          
                                      }];
    
    [task resume];
    return task;
}

- (void)sp_getUserFollowSuccess:(NSString *)followCount {
    NSLog(@"Continue");
}
@end
